package de.baumann.browser.activitys.user

import android.app.Activity
import android.content.Intent
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.IBindPhoneView
import de.baumann.browser.present.BindPhonePresenter
import de.baumann.browser.units.TimerUnit
import de.baumann.browser.utils.ViewUtil
import de.baumann.browser.utils.updatePhoneStatus
import de.baumann.browser.views.dialog.AccountChangeDialog
import kotlinx.android.synthetic.main.activity_bind_phone.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 *@author:
 *@date:2019/7/5 14:45
 *@discription: 设置手机号码
 */
class BindPhoneActivity : BaseOdinActivity<IBindPhoneView, BindPhonePresenter>(), IBindPhoneView {

    private var timer: TimerUnit? = null

    companion object {
        fun startSettingPhoneActivity(activity: Activity, oldPhone: String, requestCode: Int) {
            val intent = Intent(activity, BindPhoneActivity::class.java).apply {
                putExtra("oldPhone", oldPhone)
            }
            activity.startActivityForResult(intent, requestCode)
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_bind_phone
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        setPhoneRoot.setPadding(0, ViewUtil.getStatusBarHeight(mContext), 0, 0)
        tvOldPhone.text = if (oldPhone.isEmpty()) {
            "--"
        } else {
            oldPhone
        }

        btnBindPhone.setOnClickListener {
            if (telNum.isEmpty()) {
                showShortToast(getString(R.string.input_phone))
                return@setOnClickListener
            }

            if (smsCode.isEmpty()) {
                showShortToast(getString(R.string.input_sms_code))
                return@setOnClickListener
            }

            if (password.isEmpty()) {
                showShortToast(getString(R.string.input_password))
                return@setOnClickListener
            }
            AccountChangeDialog(mContext, View.OnClickListener {
                run {
                    presenter.bindPhone()
                }
            }).show()
        }

        tvGetCode.setOnClickListener {
            timer = TimerUnit(tvGetCode)
            timer?.startTime()
            presenter.bindPhoneGetCode()
        }
    }

    override fun createPresenter(): BindPhonePresenter {
        return BindPhonePresenter()
    }

    override fun createView(): IBindPhoneView {
        return this
    }

    override fun initData() {}

    override fun getTitleText(): String {
        return getString(R.string.change_phone_number)
    }

    override fun getOldPhone(): String {
        return intent.getStringExtra("oldPhone")
    }

    override val telNum: String
        get() = edPhone.text.trim().toString()
    override val smsCode: String
        get() = edSmsCode.text.trim().toString()

    override fun getPassword(): String {
        return edPassword.text.trim().toString()
    }

    override fun getType(): Int {
        return if (oldPhone.isEmpty()) {
            0
        } else {
            1
        }
    }

    override fun onError(errorMsgId: Int) {
        super.onError(errorMsgId)
        endTimer()
    }

    override fun onError(errorMsg: String?) {
        super.onError(errorMsg)
        endTimer()
    }

    private fun endTimer() {
        timer?.endtTime()
    }

    override fun requestSuccess(type: Int) {
        when (type) {
            -1 -> {
                showShortToast(getString(R.string.verification_code_sent))
            }

            0 -> {
                updatePhoneStatus()
                showShortToast(getString(R.string.phone_number_changed_successfully))
                setResult(Activity.RESULT_OK)
                finish()
            }
        }
    }

    override fun showBindEth(): Boolean {
        return false
    }
}
